home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_2 / issue_06 / scott / c / tree next >
Encoding:
Text File  |  1989-01-27  |  11.5 KB  |  387 lines

  1. /* >C.Tree  */
  2.  
  3. /******************************************************************************
  4.   TREE    (c) D.J.Scott 1988
  5. *******************************************************************************
  6.   Program to print ADFS directory / file list or search for files.
  7.  
  8.   Command line parameters are <directory> <filename> <options>
  9.   Syntax: *Tree [<directoryname>] [<wildcard file name>] [options]
  10.     If no parameters are given then the help text is displayed
  11.     Default directoryname is ':4.$' default wild card filename is '*'
  12.     Options are:
  13.       -c  current directory only, default includes subdirectories
  14.       -d  directory names only, default includes file names
  15. *****************************************************************************/
  16.  
  17. static char *progid = "TREE 1.10 - 27/01/89";
  18.  
  19. /*************************** INCLUDE DIRECTIVES ******************************/
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <signal.h>
  25. #include <arthur.h>
  26.  
  27. /*************************** DEFINE DIRECTIVES *******************************/
  28.  
  29. #define  ADFS_FreeSpace  0x40243
  30. #define  FALSE    0
  31. #define  TRUE     1
  32. #define  OPTTOT   5
  33.  
  34. /*************************** GLOBAL VARIABLES ********************************/
  35.  
  36. int dircnt = 0;         /* directory count */
  37. int filcnt = 0;         /* file count */
  38. int indent = 0;         /* file list indentation */
  39. int sdircnt = 0;        /* subdirectory count */
  40.  
  41. int copt = FALSE;       /* current directory only option */
  42. int dopt = FALSE;       /* directory list only option */
  43. int lopt = FALSE;       /* list option */
  44. int popt = FALSE;       /* print option */
  45. int sopt = FALSE;       /* search option */
  46.  
  47. char sname[12];         /* source file name */
  48. char sdir[120];         /* source directory name */
  49.  
  50. static char *driv = ":4.";      /* default drive */
  51. static char *root = "$";        /* root directory name */
  52. static char *wild = "*";        /* wild card file name */
  53.  
  54. static char *optlist[OPTTOT] =  /* option list */
  55. {
  56.   "-c",                 /* current directory only */
  57.   "-d",                 /* directory names only */
  58.   "-l",                 /* list in single column */
  59.   "-p",                 /* print output */
  60.   "-s",                 /* search option */
  61. };
  62.  
  63. /*************************** FUNCTION DEFINITIONS ****************************/
  64.  
  65. void chkerr(struct error *);
  66. void command(int, char *);
  67. void countfile(char *);
  68. void escape(int);
  69. void freespc(char *);
  70. void helptext(void);
  71. void readdir(char *, char *);
  72. void setoption(char *);
  73.  
  74. /******************************************************************************
  75.   main          main program
  76.         argc            number of command line parameters
  77.         argv            array of parameter string pointers
  78. ******************************************************************************/
  79. int main(argc, argv)
  80.   int argc;             /* count of command line arguments */
  81.   char *argv[];         /* pointers to argument strings */
  82. {
  83.   static char *optchar = "-";   /* option command introducer */
  84.   static char *drvchar = ":";   /* drive introducer */
  85.   char *sptr;                   /* string pointer */
  86.   int i = 1;
  87.  
  88.   signal(SIGINT, escape);
  89.  
  90.   /* process command line */
  91.   if (argc == 1)
  92.   {
  93.     helptext();         /* null parameters, print help text */
  94.     exit(0);
  95.     return(0);
  96.   }
  97.   while (argc > i)
  98.   {
  99.     if (strncmp(optchar, argv[i], 1) == 0)
  100.       setoption(argv[i]);
  101.     else if (i == 1)
  102.     {
  103.       if (strncmp(drvchar, argv[i], 1) != 0)
  104.       {
  105.         strcpy(sdir, driv);
  106.         strcat(sdir, argv[i]);
  107.       }
  108.       else
  109.         strcpy(sdir, argv[1]);
  110.     }
  111.     else if (i == 2)
  112.       strcpy(sname, argv[2]);
  113.     else if (i > 2)
  114.     {
  115.       fprintf(stderr, "\n** Unknown command parameter\n\a");
  116.       helptext();
  117.       exit(1);
  118.     }
  119.   i++;
  120.   }
  121.  
  122.   /* set default directory and wildcard file names */
  123.   if (strlen(sdir) == 0)
  124.   {
  125.     strcpy(sdir, driv);
  126.     strcat(sdir, root);
  127.   }
  128.   if (strlen(sname) == 0)
  129.     strcpy(sname, wild);
  130.  
  131.   printf("\n");
  132.   if (popt == TRUE)
  133.     vdu(2);
  134.   readdir(sdir, sname);
  135.   printf("\n%d files and %d subdirectories with name %s in %d directories\n", filcnt, sdircnt, sname, dircnt);
  136.   sptr = strtok(sdir, ".");
  137.   freespc(sptr);
  138.   if (popt == TRUE)
  139.     vdu(3);
  140.   exit(0);
  141.   return(0);
  142. }
  143.  
  144. /******************************************************************************
  145.   chkerr        checks for Operating System error and prints details if found
  146. ******************************************************************************/
  147. void chkerr(e)
  148.   struct error *e;
  149. {
  150.   if (e != 0)
  151.   {
  152.     fprintf(stderr, "\n** %s  Error &%x\n\a", e->errmess, e->errnum & 0xFFF);
  153.     exit(1);                   /* terminate as fatal error */
  154.   }
  155.   return;
  156. }
  157.  
  158. /******************************************************************************
  159.   countfile     procedure to count the number and size of files in path
  160.         path            path name string pointer
  161. ******************************************************************************/
  162. void countfile(path)
  163.   char *path;
  164. {
  165.   int i;
  166.   reg_set cnt;                  /* register set for count objects call */
  167.  
  168.   for (i = 0; i < 10; i++)
  169.     cnt.r[i] = 0;               /* clear register set */
  170.   cnt.r[0] = 28;
  171.   cnt.r[1] = (int)path;         /* path name to be counted */
  172.   cnt.r[3] = 256;
  173.   if (popt == TRUE)
  174.     vdu(3);
  175.   vdu(21);
  176.   cnt = swi(OS_FSControl, &cnt); /* perform count */
  177.   vdu(6);
  178.   if (popt == TRUE)
  179.     vdu(2);
  180.   printf("%6d files: %9d bytes\n", cnt.r[3], cnt.r[2]);
  181.   return;
  182. }
  183.  
  184. /******************************************************************************
  185.   escape
  186. ******************************************************************************/
  187. void escape(signo)
  188.   int signo;
  189. {
  190.   signal(signo, SIG_IGN);
  191.   printf("\nEscape\n");
  192.   exit(1);
  193.   return;
  194. }
  195.  
  196. /******************************************************************************
  197.   freespc       obtain and print the amount of free space left on the disc
  198.         disc            string giving the disc identity
  199. ******************************************************************************/
  200. void freespc(disc)
  201.   char *disc;
  202. {
  203.   reg_set fs;
  204.  
  205.   fs.r[0] = (int)disc;
  206.   chkerr(swix(ADFS_FreeSpace, &fs));
  207.   printf("%d bytes free on disc %s, largest space %d bytes\n", fs.r[0], disc, fs.r[1]);
  208.   return;
  209. }
  210.  
  211. /******************************************************************************
  212.   helptext      outputs help text
  213. ******************************************************************************/
  214. void helptext()
  215. {
  216.   printf("%30s  (c) 1988 D.J.Scott\n", progid);
  217.   printf("List directory / file structure or search for files\n\n");
  218.   printf("Syntax: *TREE [<:drive.directory>] [<wildcard file name>] [options]\n");
  219.   printf("  If no parameters are given then the help text is displayed\n");
  220.   printf("  Default drive and directory is :4.$, wild card file name is *\n");
  221.   printf("Options are:\n");
  222.   printf("  -c  current directory only, default includes subdirectories\n");
  223.   printf("  -d  directory names only, default includes file names\n");
  224.   printf("  -l  list files in single column with indentation\n");
  225.   printf("  -p  send all screen information to printer\n");
  226.   printf("  -s  search for all files matching the wildcard file name\n");
  227.   return;
  228. }
  229.  
  230. /******************************************************************************
  231.   readdir       reads and displays directory contents
  232.         dirnam          directory path name string pointer
  233.         filnam          (wild card) file name string pointer
  234. ******************************************************************************/
  235. void readdir(dirnam, filnam)
  236.   char *dirnam;
  237.   char *filnam;
  238. {
  239.   char pathnam[250];            /* path name string */
  240.   static char *sep = ".";       /* separator string */
  241.  
  242.   struct dirlist                /* directory list record structure */
  243.   {
  244.     int loadaddr;               /* load address */
  245.     int execaddr;               /* execution address */
  246.     int filelength;             /* file length */
  247.     int fileattrib;             /* file attributes */
  248.     int objtype;                /* object type */
  249.     char objname[12];           /* object name */
  250.   } dl;
  251.  
  252.   osgbpb_block db;
  253.  
  254.   /* read and print each directory name and details */
  255.   dircnt++;
  256.   indent += 2;                  /* increase file list indent */
  257.   if (sopt == FALSE)
  258.   {
  259.     /* print directory details */
  260.     printf("%-50s", dirnam);
  261.     countfile(dirnam);
  262.   }
  263.  
  264.   /* set up data block for osgbpb to read directory contents */
  265.   db.action = 10;               /* read directory entries */
  266.   db.file_handle = (int)dirnam; /* pointer to directory name */
  267.   db.data_addr = &dl;           /* address of data */
  268.   db.number = 1;                /* number of object names to read */
  269.   db.buf_len = sizeof (dl);     /* buffer length */
  270.   db.wild_fld = filnam;         /* wildcard name to match */
  271.   db.seq_point = 0;             /* offset of first item to read from directory */
  272.   do
  273.   {
  274.     chkerr(osgbpb(&db));        /* read directory */
  275.     if (db.number != 0 && db.seq_point >= 0)
  276.     {
  277.       if (dl.objtype == 2)      /* directory */
  278.       {
  279.         sdircnt++;
  280.         if (copt == FALSE)
  281.         {
  282.           if (lopt == TRUE)
  283.           {
  284.             strcpy(pathnam, dirnam);  /* form pathname */
  285.             strcat(pathnam, sep);
  286.             strcat(pathnam, dl.objname);
  287.             readdir(pathnam, filnam); /* read directory contents */
  288.           }
  289.           if (sopt == TRUE)
  290.             printf("%s.%s\n", dirnam, dl.objname); /* print directory name */
  291.         }
  292.       }
  293.       else
  294.       {
  295.         if (dl.objtype == 1)    /* file */
  296.         {
  297.           filcnt++;
  298.           if (dopt == FALSE)
  299.           {
  300.             if (sopt == TRUE)
  301.               printf("  %s.%s\n", dirnam, dl.objname); /* print full file name */
  302.             else if (lopt == TRUE)
  303.               printf("%*s%s\n", indent, "", dl.objname); /* print indented file name */
  304.             else
  305.               printf("  %-14s", dl.objname); /* print short file name */
  306.           }
  307.         }
  308.       }
  309.     }
  310.   }
  311.   while (db.seq_point >= 0);
  312.   if (pos () > 1)
  313.     printf("\n");
  314.  
  315.   /* set up data block for osgbpb to reread directory contents */
  316.   db.number = 1;                /* number of object names to read */
  317.   db.wild_fld = wild;           /* wildcard name to match */
  318.   db.seq_point = 0;             /* offset of first item read from directory */
  319.   do
  320.   {
  321.     chkerr(osgbpb(&db));        /* read directory */
  322.     if (db.number != 0 && db.seq_point >= 0)
  323.     {
  324.       if (dl.objtype == 2)      /* directory */
  325.       {
  326.         if (copt == FALSE && lopt == FALSE)
  327.         {
  328.           /* print contents of directory */
  329.           strcpy(pathnam, dirnam);      /* form pathname */
  330.           strcat(pathnam, sep);
  331.           strcat(pathnam, dl.objname);
  332.           readdir(pathnam, filnam);     /* read directory contents */
  333.         }
  334.       }
  335.     }
  336.   }
  337.   while (db.seq_point >= 0);
  338.   indent -= 2;
  339.   return;
  340. }
  341.  
  342. /******************************************************************************
  343.   setoption     sets the option flag determined by option string
  344.         opt             pointer to option parameter string
  345. ******************************************************************************/
  346. void setoption(opt)
  347.   char *opt;
  348. {
  349.   int found = FALSE;
  350.   int n;
  351.  
  352.   for (n = 0; n < OPTTOT; n++)
  353.   {
  354.     if (strncmp(opt, optlist[n], 2) == 0)
  355.     {
  356.       found = TRUE;
  357.       switch(n)
  358.       {
  359.       case 0 :
  360.         copt = TRUE;
  361.         break;
  362.       case 1 :
  363.         dopt = TRUE;
  364.         break;
  365.       case 2 :
  366.         lopt = TRUE;
  367.         break;
  368.       case 3 :
  369.         popt = TRUE;
  370.         break;
  371.       case 4 :
  372.         sopt = TRUE;
  373.         break;
  374.       }
  375.     }
  376.   }
  377.   if (found == FALSE)
  378.   {
  379.     fprintf(stderr, "\n** Unknown option parameter\n\a");
  380.     helptext();
  381.     exit(1);
  382.   }
  383.   return;
  384. }  
  385.  
  386. /* end of file */
  387.